//
//  AppDelegate.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/15.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //设置导航栏
        self.setNavStyle()
        //用户是否登录
        if  (YCUserAccount.loadUserAccount() != nil){
        
            //账号有值代表账号可用
            
            //为真则是新版本或者第一进入
            if  !isNewFeature(){
                
                changeRootViewController(YCNewFeatureViewController())
                
            }else{
              changeRootViewController(YCWelComeViewController())
            
            }
        
        }//用户没有登录
        else{
            changeRootViewController(YCMainViewController())
        }
       
        
        
        
        window?.makeKeyAndVisible()
        return true
    }
    //改变根控制器
     func changeRootViewController(rootController:UIViewController){
        window?.rootViewController = rootController
        
    }
    //选择根控制器
    func switchRootViewController(isMain: Bool){
    
        window?.rootViewController = isMain ? YCMainViewController() : YCWelComeViewController()
    
    }
    
    //判断是不是新版本
    private func isNewFeature() -> Bool{
    
        //取得当前版本
        let currentEdition = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        //与上一个版本比较
       let oldEdition = NSUserDefaults.standardUserDefaults().objectForKey("Edition") as? String
        
        if let old = oldEdition{
            
            if currentEdition == old
            {
                return true
            }
            
        }
        //保存当前版本
        NSUserDefaults.standardUserDefaults().setObject(currentEdition, forKey: "Edition")
        
        return false
    }
    
    
    
    //MARK: - 设置导航栏背景
    private func setNavStyle() {
    
        let nav = UINavigationBar.appearance()
        nav.tintColor = UIColor.orangeColor()
    
    }
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

